const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let m;
let fileArr = [];
let sizeArr = [];

rl.on("line", (line)=>{
    if(m === undefined) {
         m = Number(line.trim())
    } else if (fileArr.length === 0) {
        fileArr = line.trim().split(" ").map(Number)
    } else if (sizeArr.length === 0) {
        sizeArr = line.trim().split(" ").map(Number)
        console.log(getResult(m, fileArr, sizeArr))

        // clear
        m = undefined
        fileArr.length = 0
        sizeArr.length = 0
    }
})

/**
 *  求最小金币消耗数
 * @param m {number} 缓存一个文件需要金币数
 * @param fileArr {[number] } 文件序列
 * @param sizeArr {[number]} 文件大小
 * @return {number}
 */
function getResult(m, fileArr, sizeArr) {
    let obj = {}
    // 统计
    for (let i = 0; i < fileArr.length; i++) {
        let key = fileArr[i]
        if(obj.hasOwnProperty(key)) {
            obj[key]["times"]++
        } else {
            obj[key] = {
                times: 1,
                size: sizeArr[i]
            }
        }
    }

    // 数据处理, 求单文件最小消费
    let res = []
    for (const file of Object.values(obj)) {
        let cost1 = file.times * file.size  // 不缓存
        let cost2 = file.size + m // 缓存
        res.push(Math.min(cost1, cost2))
    }

    return res.reduce((acc, a)=> a + acc , 0)
}